package E_2024;

import java.util.*;

public class E_100_9 {
    /*
    输入N个互不相同的二维整数坐标，求这N个坐标可以构成的正方形数量。[内积为零的的两个向量垂直]
    输入描述
    第一行输入为N，N代表坐标数量，N为正整数。N <= 100
    之后的 K 行输入为坐标x y以空格分隔，x，y为整数，-10<=x, y<=10
    输出描述
    输出可以构成的正方形数量。
    示例 1 输入输出示例仅供调试，后台判题数据一般不包含示例
    输入
        3
        1 3
        2 4
        3 1
    输出
        0 （3个点不足以构成正方形）
     */

    public static void main(String[] args) {
        List<Point> list = new ArrayList<>();
        list.add(new Point(0,0));
        list.add(new Point(0,1));
        list.add(new Point(1,0));
        list.add(new Point(1,1));
        list.add(new Point(2,1));
        list.add(new Point(2,0));
        System.out.println(num2(list));
    }
    //通过判断两点线段之间正向或反向反转后的点是否存在在当前点位记录中
    public static int num2(List<Point> points){
        int count = 0;
        if (points.size() < 4) return count;
        Set<Point> set = new HashSet<>(points);
        for (int i = 0; i < points.size(); i++) {
            for (int k = i+1; k < points.size(); k++) {
                Point a = points.get(i);
                Point b = points.get(k);
                // 计算 向量 ab
                int[] ab = new int[]{b.x - a.x , b.y - a.y};
                // 90度翻转 ab , 得点c,d坐标
                int[] ad = new int[]{-ab[1], ab[0]};
                Point d = new Point(a.x + ad[0], a.y + ad[1]);
                Point c = new Point(b.x + ad[0], b.y + ad[1]);
                if (set.contains(d) && set.contains(c)) count++;
                // -90度翻转 ab , 得点c,d坐标
                int[] ac = new int[]{ab[1],-ab[0]};
                Point d1 = new Point(a.x + ac[0], a.y + ac[1]);
                Point c1 = new Point(b.x + ac[0], b.y + ac[1]);
                if (set.contains(d1) && set.contains(c1)) count++;
                System.out.println(a);
                System.out.println(b);
                System.out.println(Arrays.toString(ab));
                System.out.println(Arrays.toString(ad));
                System.out.println(d);
                System.out.println(c);
                System.out.println(Arrays.toString(ac));
                System.out.println(d1);
                System.out.println(c1);
                System.out.println("==========");
            }
        }
        // 正方形每一边都计算一次
        return count/4;
    }
    //通过向量内积为零判断
    public static int num(List<Point> st){
        // 按长度分组向量，正方形需要四边相等，因此长度相同的才能组成一个正方形
        int count = 0;
        for (int i = 0; i < st.size(); i++) {
            Point a = st.get(i);
            for (int j = i+1; j < st.size(); j++) {
                Point b = st.get(j);
                for (int k = j+1; k < st.size(); k++) {
                    Point c = st.get(k);
                    for (int l = k+1; l < st.size(); l++) {
                        Point d = st.get(l);
                        // 计算向量垂直，同时判断线段平行
                        if ((a.x - b.x) * (c.x - d.x) + (a.y - b.y) * (c.y - d.y) == 0 && (a.x - c.x) * (b.y - d.y) - (a.y - c.y) * (b.x - d.x) == 0 ){
                            count++;
                        } else if ((a.x - c.x) * (b.x - d.x) + (a.y - c.y) * (b.y - d.y) == 0 && (a.x - b.x) * (c.y - d.y) - (a.y - b.y) * (c.x - d.x) == 0 ) {
                            count++;
                        }else if ((a.x - d.x) * (b.x - c.x) + (a.y - d.y) * (b.y - c.y) == 0 && (a.x - c.x) * (b.y - d.y) - (a.y - c.y) * (b.x - d.x) == 0 ) {
                            count++;
                        }
                    }
                }
            }
        }
        return count;
    }
    static class Point{
        int x;
        int y;
        Point(int x, int y){
            this.x=x;
            this.y=y;
        }

        @Override
        public String toString() {
            return "x: " + x + " = y: "+y;
        }

        @Override
        public boolean equals(Object o) {
            if (o instanceof Point o1){
                return o1.x == this.x && o1.y == this.y;
            }
            return false;
        }

        @Override
        public int hashCode() {
            return 31*x+y;
        }
    }
}
